<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Controller', 'Controller');
App::uses('AuthComponent', 'Controller/Component');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
define('VIEW_IMG', '<img src="' . IMAGES_URL . 'lupa.gif" />');
define('EDIT_IMG', '<img src="' . IMAGES_URL . 'edit.gif" />');
define('DELETE_IMG', '<img src="' . IMAGES_URL . 'delete.png" />');

require_once(APPLIBS . "presentation.php");
require_once(APPLIBS . "funciones.php");

class AppController extends Controller {

    public $helpers = array('Html', 'Form', 'Session', 'Js', 'Validation');
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'categorias', 'action' => 'listar'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
			'authError' => 'No estás autorizado a acceder a esta ubicación.'
        )
    );

    function beforeFilter() {
        $modelo = $this->modelClass;

        $this->set('MODEL', $this->$modelo);
        $this->set('CONTROLLER', $this);
        $this->set('AUTH', $this->Auth);
        $this->set('USER', $this->Auth->user());
    }    

    protected function filtros() {
        $array = array();
        foreach ($this->filtros as $key => $value) {
            $array[] = $this->crearFiltro($key, $value);
        }
        return $array;
    }

    protected function filtrosActivos() {
        $array = array();
        $filtros = $this->filtros();
        foreach ($filtros as $filtro) {
            if (!empty($filtro[3])) {
                $campo = $filtro[1];
                $valor = $this->tipoCampoFiltro($filtro[0], $filtro[3], true);
                $array[] = array($campo, $valor);
            }
        }
        return $array;
    }

    function crearFiltro($campo, $title) {
        $value = "";
        $campo = str_replace(".", "-", $campo);

        if (isset($_POST[$campo])) {
            $value = $_POST[$campo];
        }

        return array($campo, $title, $this->tipoCampoFiltro($campo, $value), $value);
    }

    function tipoCampoFiltro($campo, $value, $readonly = false) {
        $modelo = $this->modelClass;
        $db = $this->$modelo;
        if (stristr($campo, "fecha") || strstr($campo, "date")) {
            return crearInput($campo, $value, 'datepicker', $readonly);
        } elseif (stristr($campo, "estado") || strstr($campo, "status")) {
            return crearSelect($campo, $value, p_estado(false), $readonly);
        } elseif (stristr($campo, "prioridad")) {
            return crearSelect($campo, $value, p_prioridad(false), $readonly);
        } elseif (stristr($campo, "origen")) {
            return crearSelect($campo, $value, p_origen_ticket(false), $readonly);
        } elseif (stristr($campo, "categoria_id")) {
            return crearSelect($campo, $value, p_categorias(false, $db), $readonly);
        }
        return crearInput($campo, $value, '', $readonly);
    }

}
